package com.meihua.baoleyi.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.RegBaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.SharedPreUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;
/**
 * Name: liming
 * Time: 2017/10/17
 */

public class UpdateLoginPwdActivity extends RegBaseActivity {
    String phone;
    @Bind(R.id.show_zhanghao)
    TextView showZhanghao;
    @Bind(R.id.show_old_pwd)
    EditText showOldPwd;
    @Bind(R.id.show_new_pwd)
    EditText showNewPwd;
    @Bind(R.id.show_config_new_pwd)
    EditText showConfigNewPwd;
    @Bind(R.id.save_newpwd)
    Button saveNewpwd;
    String user_id;
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    String userid;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_update_loginpwd;
    }

    @Override
    public void onInitView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ButterKnife.bind(this);
        textTitle.setText("重置登录密码");
        phone = SharedPreUtils.getString("phone", "", this);
        userid =myApp.getUser_id() ;
        if(phone != null && phone.length() >= 7){
            phone = phone.replace(phone.substring(3, 7), "****");
        }
        showZhanghao.setText(phone);
    }

    //重置登录密码点击事件
    @OnClick({R.id.text_back, R.id.save_newpwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_back: //返回
                finish();
                break;
            case R.id.save_newpwd:
                String old = showOldPwd.getText().toString().trim();
                String newpwd = showNewPwd.getText().toString().trim();
                String newspwds = showConfigNewPwd.getText().toString().trim();
                updateLoginPwd(userid, old, newpwd);
                break;
        }
    }
    private void updateLoginPwd(String userid, String password, String newpass) {
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token= myApp.getToken().toString();
        String sign_none="new_password="+newpass+"&old_password="+password+"&timestamp="+str+"&token="+token+"&user_id="+userid;
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();
        OkGo.post(APIServer.MYPROFILE_RESET_PASSWORD)
                .tag(this)
                .params("new_password",newpass)
                .params("old_password",password)
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .params("user_id",userid)
                .params("sign",sign_yes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("200")) {
                                    Toast.makeText(UpdateLoginPwdActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {
                                    Toast.makeText(UpdateLoginPwdActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();

                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(UpdateLoginPwdActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

}
